// pages/myActivity/myActivity.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    req: false,
    fromUtil: false,
    pageEnd: false,
    param: {
      pageNumber: 1,
      pageSize: 10
    },
    activityList: [],
    userInfoId: "",
    tabActive: 0,
    delindex:null,
  },
  // 分页
  onChange(e) {
    var index = e.detail.index;
    this.toggle("tabActive", index);
  },
  hidedel() {
    this.toggle("delindex", null);
  },
  showdel(e){
    var index = e.currentTarget.dataset.index
    console.log('index',index)
    this.toggle("delindex", index);
  },
  preventTouchMove() {},
  user(e) {
    var that = this;
    var id = e.currentTarget.dataset.createuid;
    var name = e.currentTarget.dataset.name;
    wx.navigateTo({
      url: '/pages/myCardNew/myCardNew?type=visitor&id=' + id + '&name=' + name,
    })
  },
  org(e) {
    console.log('org')
    var dataset = e.currentTarget.dataset;
    wx.navigateTo({
      url: '/pages/orgHome/orgHome?id=' + dataset.orgid
    })
  },
  activityDetail: function (e) {
    var that = this;
    var id = e.currentTarget.dataset.id;
    var type = e.currentTarget.dataset.type;
    var actType = e.currentTarget.dataset.acttype;
    var orgId = e.currentTarget.dataset.orgid;
    var actId = e.currentTarget.dataset.actid;
    var url = "";
    // if (!this.data.fromUtil){
    //   if (type == 0) {
    //     // 活动小程序
    //     url = 'pages/register/register?id=' + actId;
    //   } else if (type == 2 || type == 3 && actType != 1) {
    //     // 百日宴寿宴
    //     url = '/pages/hundredDetailed/hundredDetailed?id=' + actId + "&type=" + actType;
    //   } else if (type == 3) {
    //     // 祝福->普通活动
    //     url = '/pages/ordinaryDetails/ordinaryDetails?id=' + actId;
    //   }
    //   wx.navigateToMiniProgram({
    //     appId: type == 1 ? app.globalData.activityMiniAppId : app.globalData.blessingMiniAppId,
    //     envVersion: type == 1 ? app.globalData.activityMiniEnvVersion : app.globalData.blessingMiniEnvVersion,
    //     path: url,
    //     success(res) {
    //       console.log(res);
    //     }
    //   })


    // }else{

    if (type == 0 || type == 5 || type == 6 || (!this.data.fromUtil && (type == 2 || type == 3))){
        if (type == 0) {
          url = 'pages/register/register?id=' + actId;
        } else if (type == 5 || type == 6 || (!this.data.fromUtil && (type == 2 || type == 3))){
          var preType = "";
          if (type == 5 || type == 2){
            preType = 2;
          }else{
            preType = 3;
          }
          url = '/pages/hundredDetailed/hundredDetailed?id=' + actId + "&type=" + preType;
        }
        console.log(url)
        wx.navigateToMiniProgram({
          appId: type == 0 ? app.globalData.activityMiniAppId : app.globalData.blessingMiniAppId,
          envVersion: type == 0 ? app.globalData.activityMiniEnvVersion : app.globalData.blessingMiniEnvVersion,
          path: url,
          success(res) {
            console.log(res);
          }
        })
      }else{
        url = "/pages/contentDetails/contentDetails";
        // if (id && orgid) {
        url = url + "?id=" + actId + "&orgId=" + orgId;
        // } else if (id && orgid) {
        //   url += "?id=" + id;
        // }
        console.log(url)
        wx.navigateTo({
          url: url
        })
      }

    // }
  },
  
  // 个人活动列表
  activityDetail1: function (e) {
    var that = this;
    var id = e.currentTarget.dataset.id;
    var type = e.currentTarget.dataset.type;
    var actType = e.currentTarget.dataset.acttype;
    var url = "";
    if (type == 1) {
      url = 'pages/register/register?id=' + id;
    } else if (type == 2 && actType != 1) {
      url = '/pages/hundredDetailed/hundredDetailed?id=' + id + "&type=" + actType;
    } else if (type == 2 && actType == 1) {
      url = '/pages/ordinaryDetails/ordinaryDetails?id=' + id;
    }
    wx.navigateToMiniProgram({
      appId: type == 1 ? app.globalData.activityMiniAppId : app.globalData.blessingMiniAppId,
      envVersion: type == 1 ? app.globalData.activityMiniEnvVersion : app.globalData.blessingMiniEnvVersion,
      path: url,
      success(res) {
        console.log(res);
      }
    })

  },

  activityList: function (append) {
    var that = this;
    var url = that.data.fromUtil ? "orgmini/activity/orgDeleteList" : "orgmini/activity/userDeleteList"
    app.req({
      url: url,
      data: {
        "orgId": that.data.fromUtil ? that.data.param.orgId : "",
        "pageNumber": that.data.param.pageNumber,
        "pageSize": that.data.param.pageSize
      },
      success: function (res) {
        if (200 == res.code) {
          that.toggle("req", true);
          var list = res.data.rows
          if (list.length != 0){
            for (var item of list) {
              if (item.createTime) {
                var timestamp = Date.parse(new Date());
                var date = new Date(timestamp);
                //获取年份  
                var Y = date.getFullYear();
                if (Y == item.createTime.substring(0, 4)) {
                  item.createTime = item.createTime.substring(5, 7) + '月' + item.createTime.substring(8, 10) + '日 ' + item.createTime.substring(11, 16)
                } else {
                  item.createTime = item.createTime.substring(0, 4) + '年' + item.createTime.substring(5, 7) + '月' + item.createTime.substring(8, 10) + '日 ' + item.createTime.substring(11, 16)
                }
              }
              if (item.applyEndTime) {
                var applyEndTimeStamp = new Date(item.applyEndTime.replace(/-/g, "/")).getTime();
                var timestamp = new Date().getTime();
                if (applyEndTimeStamp < timestamp) {
                  item.endApply = true;
                } else {
                  item.endApply = false;
                }
              } else {
                item.endApply = false;
              }

              item.birth = item.actStartTime.substring(5, 7) + '月' + item.actStartTime.substring(8, 10) + '日'
              if (item.actStartTime.substring(0, 10) == item.actEndTime.substring(0, 10)) {
                item.actTime = item.actStartTime.substring(5, 7) + '月' + item.actStartTime.substring(8, 10) + '日' + item.actStartTime.substring(11, 16) + '-' + item.actEndTime.substring(11, 16);
              } else {
                item.actTime = item.actStartTime.substring(5, 7) + '月' + item.actStartTime.substring(8, 10) + '日' + item.actStartTime.substring(11, 16) + '-' + item.actEndTime.substring(5, 7) + '月' + item.actEndTime.substring(8, 10) + '日' + item.actEndTime.substring(11, 16)
              }
            }
          }
          if (!list || list.length < that.data.param.pageSize) {
            that.toggle("pageEnd", true);
          }
          that.toggle("activityList", append ? that.data.activityList.concat(list) : list);
        }
      }
    });
  },

  toggle(type, val) {
    this.setData({
      [type]: val
    });
  },
  toPage(e) {
    var dataset = e.currentTarget.dataset;
    if (dataset.band) {
      return;
    }
    var url = dataset.turn;
    if (dataset.id) {
      url += "?id=" + dataset.id
    }
    if (dataset.orgId) {
      url += "?orgId=" + that.data.id
    }
    wx.navigateTo({
      url: url
    })
  },
  // 删除
  stateChange(e) {
    var that = this;
    console.log(type)
    var type = Boolean(e.currentTarget.dataset.type * 1);
    var actType = e.currentTarget.dataset.acttype;
    var id = e.currentTarget.dataset.id;
    var url = "";
    if (that.data.fromUtil && type){
      url = "orgmini/activity/orgRestoreAct";
    } else if (that.data.fromUtil && !type){
      url = "orgmini/activity/orgCompleteAct";
    } else if (!that.data.fromUtil && type){
      url = "orgmini/activity/userRestoreAct";
    } else if (!that.data.fromUtil && !type){
      url = "orgmini/activity/userCompleteAct";
    }
    wx.showModal({
      title: '提示',
      content: type ? '请确认是否恢复' :'请确认是否彻底删除',
      confirmText:"确认",
      success(res) {
        if (res.confirm) {
          app.req({
            url: url,
            data: {
              "orgId": that.data.fromUtil ? that.data.param.orgId : "",
              "id": id,
            },
            success: function (res) {
              if (200 == res.code) {
                that.activityList();
                wx.showToast({
                  title: type ? '恢复成功' :'彻底删除成功',
                  icon: 'none'
                });
                that.toggle("delindex", null);
              }
            }
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.id) {
      this.toggle('param.orgId', options.id);
      this.toggle('fromUtil', true);
      // this.toggle('userInfoId', options.userInfoId);
      // wx.setNavigationBarTitle({
      //   title: '活动列表'
      // })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this
    app.getUserInfo(function (userInfo) {
      that.toggle("req", false);
      that.activityList();
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    var that = this;
    app.getUserInfo(function (userInfo) {
      that.toggle("req", false);
      that.toggle("param.pageNumber", 1);
      that.toggle("pageEnd", false);
      that.activityList();
    });
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    if (!that.data.pageEnd) {
      that.toggle("param.pageNumber", that.data.param.pageNumber + 1);
      that.activityList(true);
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})